package com.chaopei.znpk.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.chaopei.znpk.entity.Class;
import com.chaopei.znpk.mapper.ClassMapper;
import com.chaopei.znpk.service.ClassService;
import com.chaopei.znpk.tools.EastExcelListener;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author Zyb
 * @Date 2022/5/26 9:39
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Autowired
    ClassMapper classMapper;

    /***
     * 下载班级信息
     * @param response
     * @throws IOException
     */
    @Override
    public void download(HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //设置响应内容类型
        response.setCharacterEncoding("utf-8");//编码
        String fileName = URLEncoder.encode("班级信息表", "UTF-8").replaceAll("\\+", "%20");//设置文件名
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");//设置响应头

        List<Class> excelByPage = getClassesByPage(1);//第一页，获取第一页记录
        ExcelWriter writer = EasyExcel.write(response.getOutputStream(), Class.class).build();//获取写出流
        WriteSheet sheet = EasyExcel.writerSheet("one").build();//创建表格，设置表格页名称
        writer.write(excelByPage,sheet);//读出
        writer.finish();//关闭流
    }

    /***
     * 上传班级信息
     * @param file
     * @throws IOException
     */
    @Override
    public void upload(MultipartFile file) throws IOException {
        ExcelReader reader = EasyExcel.read(file.getInputStream(), Class.class, new EastExcelListener(classMapper)).build();//将applicantmapper传入
        ReadSheet sheet = EasyExcel.readSheet(0).build();//第一页
        reader.read(sheet);//读
        reader.finish();

    }

    /***
     * 下载班级模板
     * @param response
     * @throws IOException
     */
    @Override
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //设置响应内容类型
        response.setCharacterEncoding("utf-8");//编码
        String fileName = URLEncoder.encode("模板", "UTF-8").replaceAll("\\+", "%20");//设置文件名
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");//设置响应头
        ExcelWriter writer = EasyExcel.write(response.getOutputStream(), Class.class).build();//获取写出流
        WriteSheet sheet = EasyExcel.writerSheet("one").build();//创建表格，设置表格页名称
        writer.write(null,sheet);//读出
        writer.finish();//关闭流
    }

    /***
     * 增加一条班级信息
     * @param aClass
     */
    @Override
    public void addClasses(Class aClass) {
        classMapper.insert(aClass);
    }

    /***
     * 根据页码获取班级信息
     * @param pageon
     * @return
     */
    public List<Class> getClassesByPage(Integer pageon) {
        int limit= 5;//页面容量
        Integer begin = (pageon-1)*limit;//起始记录
        return classMapper.selectByRowBounds(new Class(),
                new RowBounds(begin,limit));//
    }

}
